package com.skyworth.web.admin.user.repository;

import com.skyworth.web.admin.user.domain.UserModel;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author holin
 * @date 11/22/15.
 */
public interface UserRepository extends PagingAndSortingRepository<UserModel, Long> {


    @Query("select user from UserModel user where  user.storeId=?1")
    List<UserModel> findByStoreId(long storeId);

    @Query("select w from UserModel w where  w.storeId=?1 and w.kind=0 and w.roleId<>2")
    List<UserModel> findWaiterByStoreId(long storeId);

    @Query("select user from UserModel user where user.employeeNo=?1 or user.username=?1 or user.mobile=?1")
    UserModel findByUsername(String username);
    UserModel findByEmployeeNo(String employeeNo);
    UserModel findByMobile(String mobile);
    UserModel findByAccessToken(String accessToken);

    @Transactional
    @Modifying
    @Query("update UserModel m set m.storeId =?2 where m.id = ?1")
    public void updateStoreId(long id, long storeId);

    @Query("SELECT CASE WHEN COUNT(u) > 0 THEN 'true' ELSE 'false' END FROM UserModel u WHERE u.mobile = ?1 and u.storeId=?2")
    boolean existsByMobile(String mobile, long store_id);
    @Query("SELECT CASE WHEN COUNT(u) > 0 THEN 'true' ELSE 'false' END FROM UserModel u WHERE u.mobile = ?1")
    boolean existsByMobile(String mobile);

    @Query("SELECT CASE WHEN COUNT(u) > 0 THEN 'true' ELSE 'false' END FROM UserModel u WHERE u.username = ?1 and u.storeId=?2")
    boolean existsByUsername(String username, long store_id);

    @Query("SELECT CASE WHEN COUNT(u) > 0 THEN 'true' ELSE 'false' END FROM UserModel u WHERE u.username = ?1")
    boolean existsByUsername(String username);

    @Query("SELECT CASE WHEN COUNT(u) > 0 THEN 'true' ELSE 'false' END FROM UserModel u WHERE u.employeeNo = ?1 and u.storeId=?2")
    boolean existsByEmployeeNo(String employeeNo, long store_id);

    @Query("select u from UserModel u where u.kind <> 0")
    Page<UserModel> users(Pageable pageRequest);

    @Query("select u from UserModel u where u.kind = 0")
    Page<UserModel> students(Pageable pageRequest);

    @Query("select user from UserModel user where  user.storeId=?1")
    Page<UserModel> findByStoreId(long storeId, Pageable pageable);

    @Query("SELECT CASE WHEN COUNT(u) > 0 THEN 'true' ELSE 'false' END FROM UserModel u WHERE u.employeeNo = ?1 and u.id <> ?2 and u.storeId=?3")
    boolean existEmployee(String employeeNo, long id, long store_id);

    @Query("SELECT CASE WHEN COUNT(u) > 0 THEN 'true' ELSE 'false' END FROM UserModel u WHERE u.username = ?1 and u.id <> ?2 and u.storeId=?3")
    boolean existUser(String username, long id, long store_id);

    @Query("SELECT CASE WHEN COUNT(u) > 0 THEN 'true' ELSE 'false' END FROM UserModel u WHERE u.mobile = ?1 and u.id <> ?2 and u.storeId=?3")
    boolean existPhone(String mobile, long id, long store_id);
}
